package com.scalar.chiptrack.adaptor.osep;

import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTML;
import javax.swing.text.html.parser.DTD;
import javax.swing.text.html.parser.DocumentParser;
import javax.swing.text.MutableAttributeSet;
import java.io.FileReader;
import java.util.ArrayList;
/**
 * User: ChandraSekhar
 * Date: Feb 20, 2007
 * Time: 5:49:38 PM
 */
public class OSEPHtmlWipParser extends HTMLEditorKit.ParserCallback
{
    private OSEPWipStatus wipStatus     = null;
    private ArrayList m_fabList         = new ArrayList();

    private int m_tableTagCount; //keeps the count of number of table tags encountered
    private boolean m_readyForParsing;  //if set to true indicates that the parser has reached the data to be parsed
    private int m_cellIndex;
    private boolean m_cellOpen;
    private boolean m_invalidRow;
    
    public OSEPHtmlWipParser()
    {
         m_tableTagCount = 0;
         m_readyForParsing = false;
         m_cellOpen = false;
         m_cellIndex = -1;
         m_invalidRow = false;
    }
    
    public ArrayList parseWipLot( FileReader reader ) throws Exception
    {
        DTD dtd = DTD.getDTD( "HTML" );
        DocumentParser documentParser = new DocumentParser( dtd );
        documentParser.parse( reader, this, true /*ignore CharSet*/ );

        for ( int i = 0; i < m_fabList.size(); i++ )
        {
            wipStatus = ( OSEPWipStatus ) m_fabList.get( i );
        }
        return m_fabList;
    }


    public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos)
    {
        // FOR PARSING THE DOWNLOADED WIP STATUS FILE
        if ( t == HTML.Tag.TABLE )
        {
            m_tableTagCount++;

            //data is present in third table tag. So setting the variable to true to indicate the start of data
            if ( m_tableTagCount == 1 )
            {
                m_readyForParsing = true;
            }
            else if ( m_tableTagCount == 2 )
            {
                //this block is entered when the table containing data is parsed
                if ( ( wipStatus != null ) && ( m_invalidRow == false ) )
                {
                    m_fabList.add( wipStatus );
                }

                m_readyForParsing = false;
            }
        }

        if ( m_readyForParsing )
        {
            if ( ( t == HTML.Tag.TR ) )
            {
                if ( ( wipStatus != null ) )
                {

                    m_fabList.add( wipStatus );
                }

                wipStatus = new OSEPWipStatus();
                m_cellIndex = -1;
                m_invalidRow = false;
            }
        }

        // Got a new cell
        if ( m_readyForParsing )
        {
            if ( ( t == HTML.Tag.TD ) )
            {
                if ( m_cellOpen == false )
                {
                    m_cellIndex++;
                    m_cellOpen = true;
                }
                else
                {
                    m_cellOpen = false;
                }
            }
        }


    }

	public void handleText (char[] data, int pos)
    {
		String text = new String(data);
                
        if ( text == null )
        {
            text = "";
        }
        else
        {
            text = text.trim();
        }

        switch ( m_cellIndex )
        {

            case 0:
                if ( !( "".equals( text ) ) && !( "Customer WIP--PCCWIPSTD1".equalsIgnoreCase( text.trim() ) ) && !("SUBJECT:".equalsIgnoreCase( text.trim() ) ) && !("FROM:".equalsIgnoreCase( text.trim() ) )
                        && !("CUSTOMER :".equalsIgnoreCase( text.trim() ) ) && !("DATE:".equalsIgnoreCase( text.trim() ) ) && !("DEVICE".equalsIgnoreCase( text.trim() ) )
                        && !("Grand Total:".equalsIgnoreCase( text.trim() ) ) && !("WT- Wafer Test".equalsIgnoreCase( text.trim() ) ) && !("WI - Wafer Ink".equalsIgnoreCase( text.trim() ) )
                        && !("VS- Visual Inspection".equalsIgnoreCase( text.trim() ) ) && !("DA- Die Attach".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") )  )
                {
                    wipStatus.setDevice( text );
                }
                break;
            case 1:
                if ( !( "".equals( text ) ) &&  !( text.startsWith("&nbsp") ) && !( "SPECTRA LINEAR".equalsIgnoreCase( text.trim() ) )
                        && !( "PKG TYPE".equalsIgnoreCase( text.trim() ) ) && !( "DA- Die Attach".equalsIgnoreCase( text.trim() ) ) && !( "WB - Wire Bond".equalsIgnoreCase( text.trim() ) )
                        && !( "PI - PreMold Inspection".equalsIgnoreCase( text.trim() ) )  && !( "MD- Molding".equalsIgnoreCase( text.trim() ) )  )
                {
                    wipStatus.setPkgType( text );
                }
                break;
            case 2:
                if ( !( "".equals( text ) ) &&  !( text.startsWith("&nbsp") )  && !( "TIME :".equalsIgnoreCase( text.trim() ) )
                       && !( "LOT NO".equalsIgnoreCase( text.trim() ) ) && !( "PC- Post Mold Cure".equalsIgnoreCase( text.trim() ) ) && !( "DT- Dejunk Trim".equalsIgnoreCase( text.trim() ) )
                        && !( "SP- Solder Plating".equalsIgnoreCase( text.trim() ) )  && !( "FS - Form Singulation".equalsIgnoreCase( text.trim() ) ) && wipStatus.getDevice() != null )
                {
                    wipStatus.setLotNo( text );
                }
                break;
            case 3:
                if ( !( "".equals( text ) ) &&  !( text.startsWith("&nbsp") ) && !( "PO NO".equalsIgnoreCase( text.trim() ) ) && !( "SubTotal:".equalsIgnoreCase( text.trim() ) )
                     && !( "BS- Buffer Stock(Pending for Test)".equalsIgnoreCase( text.trim() ) ) && !( "FT - Final Test".equalsIgnoreCase( text.trim() ) )
                        && !( "A2 - Final Visual Inspection".equalsIgnoreCase( text.trim() ) )   )
                {
                    wipStatus.setPoNo( text );
                }
                break;
            case 4:
                if ( !( "".equals( text ) ) &&  !( text.startsWith("&nbsp") ) && !( "INV".equalsIgnoreCase( text.trim() ) ) && !( "QTY".equalsIgnoreCase( text.trim() ) ) )
                {
                    wipStatus.setStageName("ASSY QUEUE" );
                    wipStatus.setInvQty( text );
                }
                break;
            case 5:
                if ( !( "".equals( text ) ) &&  !( text.startsWith("&nbsp") ) && !( "WFR".equalsIgnoreCase( text.trim() ) ) && !( "QTY".equalsIgnoreCase( text.trim() ) ) )
                {
                    wipStatus.setWfrQty( text );
                }
                break;

            case 6:
               if ( !( "".equals( text ) ) &&  !( text.startsWith("&nbsp") ) && !( "WT".equalsIgnoreCase( text.trim() ) ) )
               {
                   wipStatus.setWaferTest( text );
               }
               break;

            case 7:
                if ( !( "".equals( text ) ) &&  !( text.startsWith("&nbsp") ) && !( "WI".equalsIgnoreCase( text.trim() ) ) )
                {
                    wipStatus.setWaferInk( text );
                }
                break;

            case 8:
                if ( !( "".equals( text ) ) && !( "STOCK".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setStock( text );
                }
                break;

            case 9:
                if ( !( "".equals( text ) ) && !( "VS".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setStageName("SAW");
                    wipStatus.setVisualInspection( text );
                }
                break;
            case 10:
                if ( !( "".equals( text ) ) && !( "DA".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setStageName("D/A");
                    wipStatus.setDieAttach( text );
                }
                break;

            case 11:
                if ( !( "".equals( text ) ) && !( "WB".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setStageName("W/B");
                    wipStatus.setWireBond( text );
                }
                break;

            case 12:
                if ( !( "".equals( text ) ) && !( "PI".equalsIgnoreCase( text.trim() ) ) && !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setStageName("Mold");
                    wipStatus.setPreMoldInspection( text );
                }
                break;

            case 13:
                if ( !( "".equals( text ) ) && !( "MD".equalsIgnoreCase( text.trim() ) ) && !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setStageName("Mold");
                    wipStatus.setMolding( text );
                }
                break;

            case 14:
                if ( !( "".equals( text ) ) &&  !( text.startsWith("&nbsp") ) && !( "PC".equalsIgnoreCase( text.trim() ) ) )
                {
                    wipStatus.setStageName("Mold");
                    wipStatus.setPostMoldCure( text );
                }
                break;
            case 15:
                if ( !( "".equals( text ) ) &&  !( text.startsWith("&nbsp") ) && !( "DT".equalsIgnoreCase( text.trim() ) ) )
                {
                    wipStatus.setStageName("T/F");
                    wipStatus.setDejunkTrim( text );
                }
                break;

            case 16:
               if ( !( "".equals( text ) ) &&  !( text.startsWith("&nbsp") ) && !( "SP".equalsIgnoreCase( text.trim() ) ) )
               {
                   wipStatus.setStageName("T/F");
                   wipStatus.setSolderPlating( text );
               }
               break;

            case 17:
                if ( !( "".equals( text ) ) &&  !( text.startsWith("&nbsp") ) && !( "FS".equalsIgnoreCase( text.trim() ) ) )
                {
                    wipStatus.setStageName("T/F");
                    wipStatus.setFormSingulation( text );
                }
                break;

            case 18:
                if ( !( "".equals( text ) ) && !( "BS".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setBufferStock( text );
                }
                break;

            case 19:
                if ( !( "".equals( text ) ) && !( "FT".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setFinalTest( text );
                }
                break;

            case 20:
                if ( !( "".equals( text ) ) && !( "A2".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setFinalVisualInspection( text );
                }
                break;

            case 21:
                if ( !( "".equals( text ) ) && !( "FG".equalsIgnoreCase( text.trim() ) ) && !( "BAL".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setFgBal( text );
                }
                break;

            case 22:
                if ( !( "".equals( text ) ) && !( "RCVD".equalsIgnoreCase( text.trim() ) ) && !( "DATE".equalsIgnoreCase( text.trim() ) ) && !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setRcvdDate( text );
                }
                break;

            case 23:
                if ( !( "".equals( text ) ) && !( "INPUT".equalsIgnoreCase( text.trim() ) ) && !( "DATE".equalsIgnoreCase( text.trim() ) ) && !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setInputDate( text );
                }
                break;

            case 24:
                if ( !( "".equals( text ) ) && !( "ASSY_O".equalsIgnoreCase( text.trim() ) ) && !( "DATE".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setAssyODate( text );
                }
                break;
            case 25:
                if ( !( "".equals( text ) ) && !( "RTN".equalsIgnoreCase( text.trim() ) ) && !( "DATE".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") )  )
                {
                    wipStatus.setRtnDate( text );
                }
                break;

            case 26:
               if ( !( "".equals( text ) ) && !( "R-RTN".equalsIgnoreCase( text.trim() ) ) && !( "DATE".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") )  )
               {
                   wipStatus.setrRtnDdate( text );
               }
               break;

            case 27:
                if ( !( "".equals( text ) ) &&  !( text.startsWith("&nbsp") ) && !( "CODE".equalsIgnoreCase( text.trim() ) ) )
                {
                    wipStatus.setCode( text );
                }
                break;

            case 28:
                if ( !( "".equals( text ) ) && !( "CODE".equalsIgnoreCase( text.trim() ) ) && !( "DESCRIPTION".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setCodeDescription( text );
                }
                break;

            case 29:
                if ( !( "".equals( text ) ) && !( "PROCESS".equalsIgnoreCase( text.trim() ) ) && !( "ID".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setProcessId( text );
                }
                break;
            default:
                break;
        }
    }

    // Resets the objects state
    public void reset()
    {
        m_tableTagCount = 0;
        m_readyForParsing = false;
        m_cellOpen = false;
        m_cellIndex = -1;
        m_invalidRow = false;
    }

}
